package com.xiaoming.backend.dao.impl;

import com.xiaoming.backend.dao.IFacilityDao;
import com.xiaoming.backend.dao.mapper.FacilityPOMapper;
import com.xiaoming.backend.entity.db.example.FacilityPOExample;
import com.xiaoming.backend.entity.db.po.FacilityPO;
import com.xiaoming.backend.entity.orm.Facility;
import com.xiaoming.backend.entity.orm.Order;
import com.xiaoming.backend.util.DeepCopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FacilityDao implements IFacilityDao {
    private final FacilityPOMapper facilityPOMapper;

    @Override
    public FacilityPO findFacility(Long facilityId) {
        return facilityPOMapper.selectByPrimaryKey(facilityId);
    }

    @Override
    public void registerFacility(Facility facility) {
        FacilityPO facilityPO = DeepCopyUtil.copyByJackson(facility, FacilityPO.class);
        facilityPOMapper.insert(facilityPO);
    }

    @Override
    public List<Facility> getFacility(String facilityType) {
        FacilityPOExample e = new FacilityPOExample();
        if (!"".equals(facilityType) && facilityType != null){
            e.createCriteria().andFacilityTypeEqualTo(facilityType);
        }
        List<FacilityPO> facilityPOS = facilityPOMapper.selectByExample(e);
        return facilityPOS.stream()
                .map(po -> DeepCopyUtil.copyByJackson(po, Facility.class))
                .collect(Collectors.toList());
    }
}
